using ImageCSharp.Core.Interfaces;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Processing;
using SixLabors.ImageSharp.PixelFormats;
using System.ComponentModel.Composition;

namespace ImageCSharp.Plugins.Filters
{
    /// <summary>
    /// USM锐化滤镜 (Unsharp Mask)
    /// </summary>
    [Export(typeof(IImagePlugin))]
    [Export(typeof(IFilterPlugin))]
    public class USMSharpenFilter : IFilterPlugin
    {
        public string Name => "USM锐化";
        public string Description => "使用USM算法锐化图像";
        public string Category => "锐化";

        public Dictionary<string, object> Parameters { get; set; } = new()
        {
            { "Amount", 1.0f },
            { "Radius", 1.0f },
            { "Threshold", 0.0f }
        };

        public Image ProcessImage(Image image)
        {
            var amount = Parameters.ContainsKey("Amount") ? (float)Parameters["Amount"] : 1.0f;
            var radius = Parameters.ContainsKey("Radius") ? (float)Parameters["Radius"] : 1.0f;
            var threshold = Parameters.ContainsKey("Threshold") ? (float)Parameters["Threshold"] : 0.0f;

            // 创建新图像以避免修改原始图像
            var result = image.CloneAs<Rgba32>();

            // 使用高斯锐化替代 USM 锐化
            result.Mutate(x => x.GaussianSharpen(radius * amount));
            return result;
        }
    }

    /// <summary>
    /// 智能锐化滤镜
    /// </summary>
    [Export(typeof(IImagePlugin))]
    [Export(typeof(IFilterPlugin))]
    public class SmartSharpenFilter : IFilterPlugin
    {
        public string Name => "智能锐化";
        public string Description => "智能锐化算法，减少噪点";
        public string Category => "锐化";

        public Dictionary<string, object> Parameters { get; set; } = new()
        {
            { "Amount", 1.0f },
            { "Radius", 1.0f },
            { "NoiseReduction", 0.5f }
        };

        public Image ProcessImage(Image image)
        {
            var amount = Parameters.ContainsKey("Amount") ? (float)Parameters["Amount"] : 1.0f;
            var radius = Parameters.ContainsKey("Radius") ? (float)Parameters["Radius"] : 1.0f;

            // 创建新图像以避免修改原始图像
            var result = image.CloneAs<Rgba32>();

            // 智能锐化：先轻微模糊减少噪点，再锐化
            var noiseReduction = Parameters.ContainsKey("NoiseReduction") ? (float)Parameters["NoiseReduction"] : 0.5f;
            if (noiseReduction > 0)
            {
                result.Mutate(x => x.GaussianBlur(noiseReduction * 0.5f));
            }

            // 使用高斯锐化
            result.Mutate(x => x.GaussianSharpen(radius * amount));
            return result;
        }
    }

    /// <summary>
    /// 高斯锐化滤镜
    /// </summary>
    [Export(typeof(IImagePlugin))]
    [Export(typeof(IFilterPlugin))]
    public class GaussianSharpenFilter : IFilterPlugin
    {
        public string Name => "高斯锐化";
        public string Description => "使用高斯核进行锐化";
        public string Category => "锐化";

        public Dictionary<string, object> Parameters { get; set; } = new()
        {
            { "Sigma", 1.0f }
        };

        public Image ProcessImage(Image image)
        {
            var sigma = Parameters.ContainsKey("Sigma") ? (float)Parameters["Sigma"] : 1.0f;

            // 创建新图像以避免修改原始图像
            var result = image.CloneAs<Rgba32>();
            result.Mutate(x => x.GaussianSharpen(sigma));
            return result;
        }
    }

    /// <summary>
    /// 自定义锐化滤镜
    /// </summary>
    [Export(typeof(IImagePlugin))]
    [Export(typeof(IFilterPlugin))]
    public class CustomSharpenFilter : IFilterPlugin
    {
        public string Name => "自定义锐化";
        public string Description => "使用自定义锐化算法";
        public string Category => "锐化";

        public Dictionary<string, object> Parameters { get; set; } = new()
        {
            { "Strength", 0.5f },
            { "Clarity", 1.0f }
        };

        public Image ProcessImage(Image image)
        {
            var strength = Parameters.ContainsKey("Strength") ? (float)Parameters["Strength"] : 0.5f;
            var clarity = Parameters.ContainsKey("Clarity") ? (float)Parameters["Clarity"] : 1.0f;

            // 创建新图像以避免修改原始图像
            var result = image.CloneAs<Rgba32>();

            // 使用多次高斯锐化模拟自定义锐化
            result.Mutate(x =>
            {
                // 轻微锐化
                x.GaussianSharpen(strength * 0.5f);

                // 如果需要更高清晰度，再次应用
                if (clarity > 1.0f)
                {
                    x.GaussianSharpen(strength * 0.3f * (clarity - 1.0f));
                }
            });

            return result;
        }
    }
}